<template>
  <div>
    <img class="headerimg" src="https://xd-video-pc-img.oss-cn-beijing.aliyuncs.com/xdclass_pro/bannner/1901/learn.png"
      alt="">
    <van-cell-group>
      <van-field v-model="username" required clearable autofocus label="用户名" right-icon="question-o" placeholder="请输入用户名"
        @click-right-icon="$toast('question')" />

      <van-field v-model="password" type="password" label="密码" placeholder="请输入密码" required />
    </van-cell-group>
    <van-cell-group>
      <van-button type="primary" size="large" @click="submitForm">登陆</van-button>
    </van-cell-group>
  </div>
</template>
<script>
import { debug } from 'util';
export default {
  data () {
    return {
      username: '',
      password: '',
    }
  },
  methods: {
    async submitForm () {
      try {
        const result = await this.axios.get('/api/login', { params: { username: this.username, password: this.password } })
        if (result.code == '0') {
          this.$store.commit('setToken', result.token)
          window.localStorage.setItem('token', result.token)

          //判断路由是否带参，带参就去到重定向参数地址，否则就去首页
          if (this.$route.query.redirect) {
            this.$router.replace({ path: this.$route.query.redirect })
          } else {
            this.$router.replace({ path: '/botnav/index' })
          }

        } else {
          this.$toast.fail(result.message)
        }
      } catch (error) {
        this.$toast.fail(error)
      }

    }
  }
}
</script>
<style scoped>
.headerimg {
  height: 150px;
  width: 100%;
}
</style>
